package com.fileCenter.actions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.fileCenter.pojo.File;
import com.fileCenter.pojo.Model;
import com.fileCenter.service.FileIOService;
import com.fileCenter.service.FileService;
import com.fileCenter.service.domain.Result;

public class FileAction extends BaseAction {
	
	private FileService fileService;
	private FileIOService fileIOService;

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		Model model = getLoginModel();
		if (model == null) {
			return mapping.findForward("login");
		}
		List<File> files = fileService.getFilesByOwnerId(model.getId());
		request.setAttribute("files", files);
		request.setAttribute("loginUser", model);
		return mapping.findForward("success");
	}
	
	
	
	public ActionForward authSelf(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		Model model = getLoginModel();
		if (model == null) {
			return mapping.findForward("login");
		}
		
		String[] ids = request.getParameterValues("fileInfo");
		fileService.authFile(getFileIds(ids), model.getId());
		
		return mapping.findForward("success");
	}
	
	public ActionForward download(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		Model model = getLoginModel();
		if (model == null) {
			return mapping.findForward("login");
		}
		
		String fid = request.getParameter("fid");
		File file = fileService.downloadFile(Long.parseLong(fid), model.getId());
		if (file == null) {
			return mapping.findForward("noPermission");
		}
		fileIOService.downloadFile(file, response);
		return null;
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		Model model = getLoginModel();
		if (model == null) {
			return mapping.findForward("login");
		}
		
		String[] fids = request.getParameterValues("fid");
		List<Long> fileIds = new ArrayList<Long>();
		for (int i = 0; i < fids.length; i++) {
			String fid = fids[i];
			fileIds.add(Long.parseLong(fid));
		}
		Result result = fileService.removeFiles(fileIds, model.getId());
		if (result == null) {
			return mapping.findForward("error");
		}
		request.setAttribute("isSuccess", result.isSuccess());
		request.setAttribute("result", result);
		fileIOService.deleteFiles((List<File>) result.get("files"));
		return mapping.findForward("success");
	}
	
	public ActionForward listUnAuth(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		Model model = getLoginModel();
		if (model == null) {
			return mapping.findForward("login");
		}
		
		List<File> files = fileService.getUnAuthFilesByOwnerId(model.getId());
		request.setAttribute("files", files);
		request.setAttribute("loginUser", model);
		return mapping.findForward("success");
	}
	
	public ActionForward deleteUnAuth(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		Model model = getLoginModel();
		if (model == null) {
			return mapping.findForward("login");
		}
		
		fileService.deleteUnAuthFilesByOwnerId(model.getId());
		return mapping.findForward("success");
	}

	private List<Long> getFileIds(String[] ids) {
		if (ids == null)
			return Collections.emptyList();
		List<Long> rst = new ArrayList<Long>(ids.length);
		for (String stringId : ids) {
			rst.add(Long.parseLong(stringId));
		}
		return rst;
	}
	
	
	
	public void setFileService(FileService fileService) {
		this.fileService = fileService;
	}

	public void setFileIOService(FileIOService fileIOService) {
		this.fileIOService = fileIOService;
	}
}
